package com.bit.cfdi.web.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bit.cfdi.web.dao.SerieDeletedDAO;
import com.bit.cfdi.web.domain.SerieDeleted;
import com.bit.cfdi.web.domain.Sucursal;
@Service
public class SerieDeletedService {

	@Autowired SerieDeletedDAO serieDeletedDAO;
	
	public void save(final SerieDeleted serieDeleted) throws DataAccessException {
		serieDeletedDAO.save(serieDeleted);
	}

	public List<SerieDeleted> getAll() {
		return serieDeletedDAO.getAll();
	}

	public List<SerieDeleted> getBySucursalRFC(final Sucursal sucursal, 
			final String RFC) throws DataAccessException {
		return serieDeletedDAO.getBySucursalRFC(sucursal, RFC);
	}

	public List<SerieDeleted> getByRFCSerieFolios(final String RFC, 
			final String serieString, final String noFolioInicial, 
			final String noFolioFinal) throws DataAccessException {
		long folioInicialSerie = Long.valueOf(noFolioInicial);
		long folioFinalSerie = Long.valueOf(noFolioFinal);
		List<SerieDeleted> toReturn = new ArrayList<SerieDeleted>();
		List<SerieDeleted> listOfSeries = getBySucursalRFC(null, RFC);
		for(SerieDeleted tempSerie : listOfSeries) {
			if(tempSerie.getSerie().equals(serieString)) {
				long folioInicial = Long.valueOf(tempSerie.getNoFolioInicial());
				long folioFinal = Long.valueOf(tempSerie.getNoFolioFinal());
				if(folioInicialSerie>=folioInicial && 
						folioInicialSerie<=folioFinal) {
					toReturn.add(tempSerie);
					continue;
				}
				if(folioFinalSerie>=folioInicial 
						&& folioFinalSerie<=folioFinal) {
					toReturn.add(tempSerie);
					continue;
				}
			}
		}
		return toReturn;
	}

}
